package com.sales.service.http;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

import com.sales.service.constants.ServiceConstants;
import com.sales.service.constants.ServiceConstants.HttpMethod;
import com.sales.service.utils.CacheManager;
import com.sforce.android.soap.partner.OAuthLoginResult;

/**
 * This class is used as Http manager.
 * Http get, post and delete methods are handled here.
 * the request entity is obtained here.
 * 
 * @author Sega
 *
 */
public class HttpServiceHelper {

	/**
	 * Strings for debugging process by log.i method
	 */
	private static final String TagProject = "SalesService";
	private static final String TagPage = "Helper ::";
	
	/**
	 * ServiceRequest class is declared.
	 */
	private ServiceRequest serviceRequest;

	/**
	 * Declaring Http method, response, client
	 */
	private HttpMethod httpMethod;
	private HttpResponse httpResponse;
	private HttpClient httpClient;

	/**
	 * Constructor - gets the servicerequest class
	 * 
	 * @param request
	 */
	public HttpServiceHelper(ServiceRequest request) {
		this.serviceRequest = request;
	}

	/**
	 * method to call Http GET, POST, DELETE
	 * 
	 * @return entity is returned.
	 * 
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws URISyntaxException
	 */
	public HttpEntity execute() throws ClientProtocolException, IOException, URISyntaxException {
		switch (getHttpMethod()) {
		case HTTP_GET:
			return handleHttpGet();
		case HTTP_DELETE:
			break;
		case HTTP_POST:
			break;
		}
		return null;
	}

	/**
	 * getmethod to get the httpMethod
	 * @return
	 */
	public HttpMethod getHttpMethod() {
		if (httpMethod == null)
			httpMethod = HttpMethod.HTTP_GET;
		return httpMethod;
	}

	public void setHttpMethod(HttpMethod httpMethod) {
		this.httpMethod = httpMethod;
	}

	/**
	 * Get method is functioned.
	 * @return entity is returned
	 * 
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws URISyntaxException
	 */
	private HttpEntity handleHttpGet() throws ClientProtocolException, IOException, URISyntaxException {
		
		Object result = CacheManager.getInstance().getProperty(ServiceConstants.CACHE_OAUTH_KEY);
		OAuthLoginResult results = (OAuthLoginResult) result;
		HttpGet httpGet=null;
		httpGet = new HttpGet(new URI(serviceRequest.getRequesturl()));
		httpGet.setHeader("Authorization","OAuth "+results.getAccessToken());
		try {
			httpResponse = getHttpClient().execute(httpGet);
		} catch (UnknownHostException e){
			Log.i(TagProject, TagPage + "No internet Connection.");
			Log.i(TagProject, TagPage + "handleHttpGet: " + e.getMessage() + ", " + e.getCause() + ", " + e.getClass());
			e.printStackTrace();
		}
		return httpResponse.getEntity();
	}

	/**
	 * get method to get the Http Client.
	 * 
	 * @return Http Client is returned
	 */
	private HttpClient getHttpClient() {
		if (httpClient == null) {
			httpClient = new DefaultHttpClient();
		}
		return httpClient;
	}
	
}